package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckItemDeleteFailException;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public Result add(CheckItem checkItem) {

        try {
            checkItemDao.insert(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);

    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckItem> checkItemList;
        if (queryPageBean.getQueryString() != null && !"".equals(queryPageBean.getQueryString())) {
            checkItemList = checkItemDao.findAllQueryString(queryPageBean.getQueryString());
        } else {
            checkItemList = checkItemDao.findAll();
        }
        PageInfo pageInfo = new PageInfo<>(checkItemList);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), checkItemList);
        return pageResult;
    }

    @Override
    public Result findAllList() {
        List<CheckItem> checkItemList = null;
        try {
            checkItemList = checkItemDao.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, checkItemList);
        }

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);

    }

    @Override
    public Result update(CheckItem checkItem) {
        try {
            checkItemDao.update(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }

        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);


    }

    @Override
    public Result findById(Integer id) {
        CheckItem checkItem = null;
        try {
            checkItem = checkItemDao.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, checkItem);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    @Override
    public Result delete(Integer id) {
        try {
            Integer integer = checkItemDao.countCheckgroupId(id);
            if(integer > 0){
                throw new CheckItemDeleteFailException();
            }
            checkItemDao.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof CheckItemDeleteFailException){
                return new Result(false, "已经有检查组关联此检查项, 请优先删除检查组后再操作");
            }
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);

    }
}
